package cn.rxls.admin.shop.controller;

import cn.rxls.admin.shop.dto.PageShopDTO;
import cn.rxls.admin.shop.entity.Result;
import cn.rxls.admin.shop.entity.Shop;
import cn.rxls.admin.shop.service.ShopService;
import cn.rxls.admin.shop.vo.PageShopVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 商品(Shop)表控制层
 */
@RestController
@RequestMapping("shop")
public class ShopController {
    /**
     * 服务对象
     */
    @Resource
    private ShopService shopService;

    /**
     * 分页查询所有数据
     *
     * @param dto 参数对象
     * @return 所有数据
     */
    @GetMapping("/page")
    public Result<PageShopVO> selectAll(@ModelAttribute PageShopDTO dto) {
        PageShopVO page = shopService.page(dto);
        if (page.getTotal() == 0 && dto.getPage() == 0) {
            return Result.success("没有查询到该记录", page);
        }
        if (page.getTotal() == 0) {
            return Result.success("没有更多数据了", page);
        }
        return Result.success(page);
    }


    /**
     * 新增数据
     *
     * @param shop 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result<Boolean> insert(@Validated @RequestBody Shop shop) {
        if (Objects.isNull(shop.getMakers())
                || shop.getMakers().equals("")
                || Objects.isNull(shop.getTitle())
                || shop.getTitle().equals("")) {
            return Result.success("新增失败", false);
        }
        boolean save = false;
        try {
            save = shopService.save(shop);
        } catch (Exception e) {
            return Result.success("新增失败", false);
        }
        return Result.success("新增成功", save);
    }

    /**
     * 修改数据
     *
     * @param shop 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody Shop shop) {
        if (Objects.isNull(shop.getMakers())
                || shop.getMakers().equals("")
                || Objects.isNull(shop.getTitle())
                || shop.getTitle().equals("")) {
            return Result.success("修改失败", false);
        }
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Shop::getShopId, shop.getShopId());
        boolean put = false;
        try {
            put = shopService.update(shop, wrapper);
        } catch (Exception e) {
            return Result.success("修改失败", false);
        }
        return Result.success("修改成功", put);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        boolean del = false;
        try {
            del = shopService.removeByIds(idList);
        } catch (Exception e) {
            return Result.success("修改失败", false);
        }
        return Result.success("删除成功", del);
    }


    /**
     * 购买下单
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @PutMapping("/buy")
    public Result<Boolean> buyShop(@RequestParam("id") Long id) {
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Shop::getShopId, id);
        Shop one = shopService.getOne(wrapper);
        if (Objects.isNull(one)) {
            return Result.error("商品不存在");
        }
        if (one.getStatue().equals(0)) {
            return Result.error("商品已下架");
        }
        Integer num = one.getNum();
        one.setVolume(one.getVolume() + 1);
        if (num.equals(0)) {
            return Result.error("库存不足");
        }
        one.setNum((num - 1));
        boolean buy = false;
        try {
            buy = shopService.update(one, wrapper);
        } catch (Exception e) {
            return Result.success("修改失败", false);
        }
        return Result.success("购买成功", buy);
    }
}

